package com.wondersgroup.core.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wondersgroup.core.mapper.ProductMapper;
import com.wondersgroup.core.mapper.ProductPlatformMapper;
import com.wondersgroup.core.model.Attachment;
import com.wondersgroup.core.resultmap.DTOProduct;
import com.wondersgroup.core.util.TraceConstant;

@Service
public class ProductService {

	@Autowired
	private ProductMapper productMapper;
	
	@Autowired
	private ProductInfoService productInfoService;
	
	@Autowired
	private UtilService utilService;
	
	/**
	 * 根据条形码查找产品图片
	 * @param barcode
	 * @return 
	 */
	public DTOProduct findProductByBarcode(String barcode){
		
		DTOProduct dtoProduct=productMapper.findByBarcode(barcode);
		if(dtoProduct!=null){
			String manufacture = dtoProduct.getManufacture();
		    String manufactureAddress = dtoProduct.getManufactureAddress();
		    String productLicense=dtoProduct.getProductionLicense();
			List<Map<Object,Object>> companyInfo = productInfoService.findCompanyByBarcode(barcode);
			Map<Object,Object> map=new HashMap<Object,Object>();
			if(manufacture!=null&&manufacture!=""){
				    map.put("companyRegAddress", manufactureAddress);
				    map.put("companyName", manufacture);
				    map.put("foodProdCert", productLicense);
				    map.put("companyType", 3001);
				    companyInfo.add(map);
			}
			dtoProduct.setCompanyInfo(companyInfo);
			Attachment attachment = new Attachment();
			attachment.setObjType(TraceConstant.ATT_PRODUCT);
			attachment.setObjId(dtoProduct.getId());
			List<Attachment> pics = utilService.queryAttFile(attachment);
			if( pics!=null && pics.size()>0){
				dtoProduct.setPics(pics);
			}
			return dtoProduct;
		}else{
			return null;
		}
	
	}
	
	/**
	 * 获取可追溯产品数量
	 * @return
	 */
	public long getCountOfTraceableProductTypes() {
		long count = productMapper.getCountOfTraceableProductTypes();
		return count;
	}
	
}
